﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NLog;
using OSA.Policlinic.UI.WFApplicationWCF.RegistryServiceReference;

namespace OSA.Policlinic.UI.WFApplication
{
    public partial class choosePatientForm : Form
    {
        private IRegistryBusinessApplication registryBusinessApplication;
        private List<Patient> patients;
        public Patient ChoosedPatient;
        private static Logger logger = LogManager.GetCurrentClassLogger();

        public choosePatientForm()
        {
            InitializeComponent();
            patients=new List<Patient>();
            registryBusinessApplication = RegistryBusinessApplicationHome.GetInstanse();
            patients.AddRange(registryBusinessApplication.GetAllPatients());
            patientBindingSource.DataSource = patients;
            logger.Trace("Форма choosePatientForm успешно инициализирована");
        }

        private void patientDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                ChoosedPatient = (Patient)patientDataGridView.Rows[e.RowIndex].DataBoundItem;
                if (ChoosedPatient != null)
                {
                    logger.Debug("Для выдачи талона выбран пациент id = {0}", ChoosedPatient.Id);
                    this.Close();
                }
            }
        }

        private void SearchBySurnameButton_Click(object sender, EventArgs e)
        {
            patients.AddRange(registryBusinessApplication.GetPatientsBySurname(SurnameTextBox.Text));
            patientDataGridView.Rows.Clear();
            patientBindingSource.DataSource = patients;
            logger.Debug("Произведен поиск пациентов по фамилии {0}", SurnameTextBox.Text);
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            patients.AddRange(registryBusinessApplication.GetAllPatients());
            patientBindingSource.DataSource = patients;
        }

        private void PatientSearchButton_Click(object sender, EventArgs e)
        {
           patients.AddRange(registryBusinessApplication.GetPatientByPolisNumber(SeriesTextBox.Text, NumberTextBox.Text));
            patientDataGridView.Rows.Clear();
            patientBindingSource.DataSource = patients;
            logger.Debug("Произведен поиск пациента по полису серия {0} номер {1}", SeriesTextBox.Text, NumberTextBox.Text);
        }
    }
}
